package com.penghaisoft.microwms.product.web.controller.outwarehouse;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProSellOutSn;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.util.ExcelExpUtil;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProSellOutSnService;

@RestController
@RequestMapping(value = "/proselloutsn")
@Slf4j
public class ProSellOutSnController extends BaseController {
	
		@Autowired
		private IProSellOutSnService proSellOutSnService;
		
		@Autowired
		private IBaseDictionaryService baseDictionaryService;

	  /**
	   * 新增记录
	   * @param proSellOutSn
	   * @return
	   */
		@PostMapping("c")
		public TransResult create(ProSellOutSn proSellOutSn) {
			proSellOutSn.preInsert();
			Resp resp =  proSellOutSnService.create(proSellOutSn);
			return fromResp(resp);
		}

	  /**
	   * 删除记录（逻辑删除active_flag=0)
	   * @param proSellOutSn
	   * @return
	   */
		@PostMapping("d")
		public TransResult delete(ProSellOutSn proSellOutSn) {
			proSellOutSn.preUpdate();
			Resp resp =  proSellOutSnService.delete(proSellOutSn);
			return fromResp(resp);
		}

	  /**
	   * 查询列表
	   * @param proSellOutSn
	   * @return
	   */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,ProSellOutSn proSellOutSn) {
			Pager<ProSellOutSn> result = proSellOutSnService.findListByCondition(page,rows,proSellOutSn);
			
			String sumInfo = result.getOrder();
			if (sumInfo != null && sumInfo.length() > 0) {
				result.setOrder(sumInfo);
			}
			
			return success(result);
		}
		
		
		/** 
		* @Title: listSnExp 
		* @Description: 销售出库明细导出 
		* @param @param proSellOutSn
		* @param @param request
		* @param @param response
		* @param @return  参数说明 
		* @return String    返回类型
		* @author jzh 
		* @throws 
		*/
		@GetMapping("listSnExp")
		public String listSnExp(ProSellOutSn proSellOutSn, HttpServletRequest request, HttpServletResponse response){
			
			List<ProSellOutSn> dataList = proSellOutSnService.findListExp(proSellOutSn);
			
			BaseDictionary baseDictionary1 = new BaseDictionary();
			baseDictionary1.setActiveFlag("1");
			baseDictionary1.setDicType("model_type_p");
			List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
			if (dataList != null && !dataList.isEmpty()) {
				for (ProSellOutSn tmpData : dataList) {
					if (list1 != null && !list1.isEmpty()) {
						for (BaseDictionary tmp : list1) {
							// 判断型号类型
							if (tmp.getDicCode().equals(tmpData.getModelType())) {
								tmpData.setModelType(tmp.getDicName());
							}
						}
					}
				}
			}
			
			List<Map> columnList = new ArrayList<Map>();
			Map<String,String> columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "销售出库单号");
			columnMap.put("columnBeanName", "sellOutNo");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "合同编号");
			columnMap.put("columnBeanName", "contractNo");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "条码");
			columnMap.put("columnBeanName", "barcode");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "型号");
			columnMap.put("columnBeanName", "model");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "型号类型");
			columnMap.put("columnBeanName", "modelType");
			columnList.add(columnMap);

			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "单位");
			columnMap.put("columnBeanName", "unit");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "数量");
			columnMap.put("columnBeanName", "amount");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "库位");
			columnMap.put("columnBeanName", "wlCode");
			columnList.add(columnMap);

			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "扫描时间");
			columnMap.put("columnBeanName", "gmtCreate");
			columnList.add(columnMap);

			
			SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);
			
			OutputStream out = null;
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String sdfDate = sdf.format(date);
			String fileName = "成品销售出库明细"+sdfDate+".xlsx";// 文件名
			try {
				out = response.getOutputStream();
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
				sw.write(out);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return "sucess";
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@GetMapping("r/{id}")
		public TransResult queryById(@PathVariable String id) {
			ProSellOutSn  proSellOutSn = proSellOutSnService.findById(id);
			return success(proSellOutSn);
		}

	  /**
	   * 修改记录
	   * @param proSellOutSn
	   * @return
	   */
		@PostMapping("u")
		public TransResult update(ProSellOutSn proSellOutSn) {
			proSellOutSn.preUpdate();
			Resp resp =  proSellOutSnService.update(proSellOutSn);
	    	return fromResp(resp);
		}

}
